<template>
  <div>
    <div class="search" v-show="flag">
      <template>
        <div style="width: 100%">
          <el-form>
            <el-row>
              <el-col :span="6">
                <div class="contain">
                  标签名称：<el-input
                    clearable
                    v-model="input"
                    style="width: 198px"
                    placeholder="请输入标签名称"
                  ></el-input>
                </div>
              </el-col>
              <el-col :span="6">
                <div class="contain">
                  状态：<el-select
                    clearable
                    v-model="value"
                    placeholder="请选择 状态"
                  >
                    <el-option
                      v-for="item in options"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                </div>
              </el-col>
              <el-col :span="6" style="text-align: right">
                <el-button type="primary" @click="search">
                  <i class="el-icon-search"></i>
                  <span>搜 索</span>
                </el-button>
                <el-button @click="qingkong">
                  <i class="el-icon-delete"></i>
                  <span>清 空</span>
                </el-button>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </template>
    </div>
    <div class="content">
      <div class="menu" style="margin-top: 20px">
        <div class="left">
          <template>
            <el-form>
              <el-button
                type="primary"
                class="but"
                @click="addOrUpdateHandle()"
              >
                <i class="el-icon-plus"></i>
                <span>新增</span>
              </el-button>
            </el-form>
          </template>
        </div>
        <div class="right">
          <el-button class="el" @click="wu">
            <i class="el-icon-refresh"></i>
          </el-button>
          <el-button class="el" @click="down">
            <i class="el-icon-search"></i>
          </el-button>
        </div>
      </div>
      <div class="table">
        <el-table
          :data="tableData"
          stripe
          border
          style="width: 100%"
          :header-cell-style="{
            fontSize: '12px',
            backgroundColor: '#eef1f6',
            color: '#333',
          }"
        >
          <el-table-column align="center" label="序号" width="49">
            <template slot-scope="scope">
              <span>{{ scope.row.serNum }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="标签名称" width="213">
            <template slot-scope="scope">
              <span>{{ scope.row.title }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="状态" width="212">
            <template slot-scope="scope">
              <el-tag size="mini" v-show="scope.row.status == 0" type="danger"
                >禁用</el-tag
              >
              <el-tag size="mini" v-show="scope.row.status == 1">正常</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" label="默认类型" width="213">
            <template>
              <el-tag>自定义类型</el-tag>
              <!-- <el-tag></el-tag> -->
            </template>
          </el-table-column>
          <el-table-column align="center" label="排序" width="212">
            <template slot-scope="scope">
              <span>{{ scope.row.seq }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作">
            <template slot-scope="scope">
              <el-button
                type="primary"
                @click="() => update(scope.$index, scope.row)"
              >
                <i class="el-icon-edit"></i>
                <span>修改</span>
              </el-button>
              <el-button type="danger" @click="del(scope.$index, scope.row)">
                <i class="el-icon-delete"></i>
                <span>删除</span>
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="block">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 40, 50, 100]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>

    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./prodTag-add-or-update.vue";
export default {
  data() {
    return {
      flag: true,
      addOrUpdateVisible: false,
      input: "",
      options: [
        {
          value: 0,
          label: "禁用",
        },
        {
          value: 1,
          label: "正常",
        },
      ],
      value: "",
      tableData: [],
      params: {
        current: 1,
        size: 10,
        title: "",
        status: "",
      },
      // 分页组
      currentPage: 1,
      total: 0,
    };
  },
  components: {
    AddOrUpdate,
  },
  mounted() {
    this.getDataList();
  },
  methods: {
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    getDataList() {
      this.$http
        .get("apis/prod/prodTag/page", {
          params: this.params,
        })
        .then((res) => {
          // console.log(res);
          this.tableData = res.data.records;
          for (let i = 0; i < this.tableData.length; i++) {
            this.$set(this.tableData[i], "serNum", i + 1);
          }
          this.total = res.data.total;
          console.log(this.tableData);
        });
    },
    search() {
      console.log(this.input, this.value);

      this.params.title = this.input;
      this.params.status = this.value;
      this.getDataList();
    },
    qingkong() {
      this.input = "";
      this.value = "";
    },
    wu() {
      this.params.current = 1
      this.params.size = 10
      this.params.status = ""
      this.params.title = ""
      this.getDataList();
    },
    down() {
      if (this.flag == true) {
        this.flag = false;
      } else {
        this.flag = true;
      }
    },
    del(index, row) {
      console.log(index, row);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http.delete("apis/prod/prodTag/" + row.id).then((res) => {
            console.log(res);
            this.getDataList();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 分页组
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.params.current = 1;
      this.params.size = val;
      this.handleCurrentChange(1);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.params.current = val;
      this.getDataList();
    },
    // 修改
    update(index, row) {
      // console.log(index,row.id);
      this.addOrUpdateHandle(row.id);
    },
  },
};
</script>

<style scoped>
.el {
  padding: 12px;
  border-radius: 50%;
}
.inp {
  width: 100%;
  /* display: flex; */
}
.contain {
  display: flex;
  justify-content: space-evenly;
  align-items: center;
}
.el-col {
  margin-right: 10px;
}
.menu {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}
.right .but {
  border-radius: 50%;
  width: 30px;
  height: 30px;
  border: 1px solid #000;
  background-color: #fff;
  margin: 0 8px;
}
.tag {
  margin-bottom: 10px;
}
.proImg img {
  height: 40px;
}
.block {
  margin-top: 20px;
}
</style>